import { BasicColumn } from '/@/components/Table'
import { FormSchema } from '/@/components/Table'
import { getAllRolesListNoByTenant, getAllTenantList } from './user.api'
import { rules } from '/@/utils/helper/validator'
import { h } from 'vue'
import { render } from '/@/utils/common/renderUtils'
export const columns: BasicColumn[] = [
	{
		title: '用户账号',
		dataIndex: 'code',
		width: 120,
	},
	{
		title: '姓名',
		dataIndex: 'name',
		width: 100,
	},
	{
		title: '角色',
		dataIndex: 'roleName',
		width: 100,
	},
	{
		title: '创建时间',
		dataIndex: 'createdTime',
		sorter: true,
		width: 150,
	},
	{
		title: '启用',
		width: 50,
		sorter: true,
		customRender: ({ record }) => {
			return h('span', {}, `${record.status ? '启用' : '停用'}`)
		},
	},
]

export const recycleColumns: BasicColumn[] = [
	{
		title: '用户账号',
		dataIndex: 'username',
		width: 100,
	},
	{
		title: '用户姓名',
		dataIndex: 'realname',
		width: 100,
	},
	{
		title: '头像',
		dataIndex: 'avatarUrl',
		width: 80,
		customRender: render.renderAvatar,
	},
	{
		title: '性别',
		dataIndex: 'sex',
		width: 80,
		sorter: true,
		customRender: ({ text }) => {
			return render.renderDict(text, 'sex')
		},
	},
]

export const searchFormSchema: FormSchema[] = [
	{
		label: '关键字',
		field: 'keyword',
		component: 'Input',
		colProps: { span: 6 },
		componentProps: {
			placeholder: '请输入用户名称/编号',
		},
	},
]

export const formSchema: FormSchema[] = [
	{
		label: '',
		field: 'id',
		component: 'Input',
		show: false,
	},
	{
		label: '用户账号',
		field: 'code',
		required: true,
		component: 'Input',
		dynamicDisabled: ({ values }) => {
			return !!values.id
		},
	},
	{
		label: '登录密码',
		field: 'password',
		component: 'StrengthMeter',
		helpMessage: '至少使用大小写字母、数字、符号两种类型组合的密码，长度至少为8位',
	},
	// {
	// 	label: '确认密码',
	// 	field: 'confirmPassword',
	// 	component: 'InputPassword',
	// 	dynamicRules: ({ values }) => rules.confirmPassword(values, false),
	// },
	{
		label: '用户姓名',
		field: 'name',
		required: true,
		component: 'Input',
	},
	{
		label: '角色',
		field: 'roleId',
		required: true,
		component: 'rwSelect',
		componentProps: {
			type: 'role',
		},
	},
	{
		label: '头像',
		field: 'avatar',
		component: 'rwUpload',
		componentProps: {
			fileMax: 1,
		},
	},
	{
		label: '性别',
		field: 'sex',
		component: 'rwDictSelect',
		componentProps: {
			code: 'sex',
		},
	},
	{
		label: '邮箱',
		field: 'email',
		component: 'Input',
		dynamicRules: () => {
			return [ { ...rules.rule('email', false)[0] } ]
		},
	},
	{
		label: '手机号码',
		field: 'phoneNumber',
		component: 'Input',
		dynamicRules: () => {
			return [ { pattern: /^1[3456789]\d{9}$/, message: '手机号码格式有误' } ]
		},
	},
	{
		label: '是否有效',
		field: 'status',
		component: 'Switch',
		defaultValue: true,
	},
]

export const formPasswordSchema: FormSchema[] = [
	{
		label: '登录密码',
		field: 'password',
		component: 'StrengthMeter',
		componentProps: {
			placeholder: '请输入登录密码',
		},
		rules: [
			{
				required: true,
				message: '请输入登录密码',
			},
		],
	},
	{
		label: '确认密码',
		field: 'confirmPassword',
		component: 'InputPassword',
		dynamicRules: ({ values }) => rules.confirmPassword(values, true),
	},
]

export const formAgentSchema: FormSchema[] = [
	{
		label: '',
		field: 'id',
		component: 'Input',
		show: false,
	},
	{
		field: 'userName',
		label: '用户名',
		component: 'Input',
		componentProps: {
			readOnly: true,
			allowClear: false,
		},
	},
	{
		field: 'agentUserName',
		label: '代理人用户名',
		required: true,
		component: 'JSelectUser',
		componentProps: {
			rowKey: 'username',
			labelKey: 'realname',
			maxSelectCount: 10,
		},
	},
	{
		field: 'startTime',
		label: '代理开始时间',
		component: 'DatePicker',
		required: true,
		componentProps: {
			showTime: true,
			valueFormat: 'YYYY-MM-DD HH:mm:ss',
			placeholder: '请选择代理开始时间',
		},
	},
	{
		field: 'endTime',
		label: '代理结束时间',
		component: 'DatePicker',
		required: true,
		componentProps: {
			showTime: true,
			valueFormat: 'YYYY-MM-DD HH:mm:ss',
			placeholder: '请选择代理结束时间',
		},
	},
	{
		field: 'status',
		label: '状态',
		component: 'JDictSelectTag',
		defaultValue: '1',
		componentProps: {
			dictCode: 'valid_status',
			type: 'radioButton',
		},
	},
]

export const formQuitAgentSchema: FormSchema[] = [
	{
		label: '',
		field: 'id',
		component: 'Input',
		show: false,
	},
	{
		field: 'userName',
		label: '用户名',
		component: 'Input',
		componentProps: {
			readOnly: true,
			allowClear: false,
		},
	},
	{
		field: 'agentUserName',
		label: '交接人员',
		required: true,
		component: 'JSelectUser',
		componentProps: {
			rowKey: 'username',
			labelKey: 'realname',
			maxSelectCount: 1,
		},
	},
	{
		field: 'startTime',
		label: '交接开始时间',
		component: 'DatePicker',
		required: true,
		componentProps: {
			showTime: true,
			valueFormat: 'YYYY-MM-DD HH:mm:ss',
			placeholder: '请选择交接开始时间',
			getPopupContainer: () => document.body,
		},
	},
	{
		field: 'endTime',
		label: '交接结束时间',
		component: 'DatePicker',
		required: true,
		componentProps: {
			showTime: true,
			valueFormat: 'YYYY-MM-DD HH:mm:ss',
			placeholder: '请选择交接结束时间',
			getPopupContainer: () => document.body,
		},
	},
	{
		field: 'status',
		label: '启用',
		component: 'JDictSelectTag',
		defaultValue: '1',
		componentProps: {
			dictCode: 'valid_status',
			type: 'radioButton',
		},
	},
]

//租户用户列表
export const userTenantColumns: BasicColumn[] = [
	{
		title: '用户账号',
		dataIndex: 'username',
		width: 120,
	},
	{
		title: '用户姓名',
		dataIndex: 'realname',
		width: 100,
	},
	{
		title: '头像',
		dataIndex: 'avatar',
		width: 120,
		customRender: render.renderAvatar,
	},
	{
		title: '手机号',
		dataIndex: 'phone',
		width: 100,
	},
	{
		title: '部门',
		width: 150,
		dataIndex: 'orgCodeTxt',
	},

	{
		title: '状态',
		dataIndex: 'status',
		sorter: true,
		width: 80,
		customRender: ({ text }) => {
			if (text === '1') {
				return '正常'
			} else if (text === '3') {
				return '审批中'
			} else {
				return '已拒绝'
			}
		},
	},
]

//用户租户搜索表单
export const userTenantFormSchema: FormSchema[] = [
	{
		label: '账号',
		field: 'username',
		component: 'Input',
		colProps: { span: 6 },
	},
	{
		label: '名字',
		field: 'realname',
		component: 'Input',
		colProps: { span: 6 },
	},
	{
		label: '性别',
		field: 'sex',
		component: 'JDictSelectTag',
		componentProps: {
			dictCode: 'sex',
			placeholder: '请选择性别',
			stringToNumber: true,
		},
		colProps: { span: 6 },
	},
]
